import os
import yaml
from loguru import logger
from utils.object_utils import cache


# 返回项目路径，或者返回某个目录的路径
def get_path(dir,file_name=None):
    project_path = os.path.dirname(os.path.dirname(__file__))
    path = os.path.join(project_path,dir)
    print("path:",path)
    if file_name is not None:
        path = os.path.join(path,file_name)

    return path


# 读取yml文件，返回python对象
def read_yaml_file(yml_file):
    yml_path = get_path('data',yml_file)
    with open(yml_path,encoding='utf-8') as yml_obj:
        data = yaml.safe_load(yml_obj)
        logger.info("返回的测试数据为:{}".format(data))
        return data


# 判断传入的值是否包含了{{ }}
def is_contains_brace(value):
    start_index = value.find("{{")
    if start_index >=0:
        end_index = value.find("}}")
        vls = cache.get(value[start_index+2:end_index])
        logger.debug("从缓存中提取到的健名为:{},值为:{}".format(value[start_index+2:end_index],vls))
        return vls

    return value


if __name__ == '__main__':
    print(get_path("data",'user_add_flow.yml'))
    result = read_yaml_file('user_add_flow.yml')
    print(result)